package com.javaAdmin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 多线程-线程池配置
 *
 * @author Administrator
 */
@Configuration
public class ThreadPollConfig {

    //获取CPU的核心数
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();

    //核心线程数量
    private static final int COUR_SIZE = CPU_COUNT * 1;

    private static final int MAX_COUR_SIZE = CPU_COUNT * 2;

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(COUR_SIZE);  //核心线程数量
        threadPoolTaskExecutor.setMaxPoolSize(MAX_COUR_SIZE);   //最大线程数量
        threadPoolTaskExecutor.setQueueCapacity(MAX_COUR_SIZE * 4);  //队列容量
        //threadPoolTaskExecutor.setKeepAliveSeconds(60); //线程池维护线程所允许的空闲时间
        threadPoolTaskExecutor.setThreadNamePrefix("demo-thread-pool"); //线程池的名字前缀
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //当队列满了以后，如何处理新任务
        //threadPoolTaskExecutor.initialize(); //初始化
        return threadPoolTaskExecutor;
    }
}
